import request from '@/utils/request'
import {downloadFromResponse} from "@/api/downloadUtil";

// 查询设备台账维护列表
export function listAmm(query) {
  return request({
    url: '/amm/amm/list',
    method: 'get',
    params: query
  })
}

// 查询设备台账维护详细
export function getAmm(id) {
  return request({
    url: '/amm/amm/' + id,
    method: 'get'
  })
}

// 新增设备台账维护
export function addAmm(data) {
  return request({
    url: '/amm/amm',
    method: 'post',
    data: data
  })
}

// 修改设备台账维护
export function updateAmm(data) {
  return request({
    url: '/amm/amm',
    method: 'put',
    data: data
  })
}

// 删除设备台账维护
export function delAmm(id) {
  return request({
    url: '/amm/amm/' + id,
    method: 'delete'
  })
}



// 导出
export function exportAmm() {
  request({
    url: '/amm/amm/export',
    method: 'post',
    responseType: 'blob'
  }).then(response => {
    downloadFromResponse(response, "仪器仪表测量清单 " + (getNowTimeString()) + ".xls")
  })

}

// 获取导入模板
export function getImportTemplate() {
  request({
    url: '/amm/amm/getImportTemplate',
    method: 'get',
    responseType: 'blob'
  }).then(response => {
    downloadFromResponse(response, "仪器仪表-导入模板.xls")
  })

}

// 导入
export function importAmm(file) {
  let f = new FormData()
  f.append('file',file)
  return request({
    url: '/amm/amm/import' ,
    method: 'post',
    data:f
  })
}
function getNowTimeString(){
  return ""+fmtTime(new Date(),'yyyy-MM-dd hh:mm:ss')
}

function fmtTime (time,fmt) {
  let o = {
    "M+": time.getMonth() + 1, //月份
    "d+": time.getDate(), //日
    "h+": time.getHours(), //小时
    "m+": time.getMinutes(), //分
    "s+": time.getSeconds(), //秒
    "q+": Math.floor((time.getMonth() + 3) / 3), //季度
    "S": time.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (let k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
